import type { REPORT_ID } from '@/enum'
import { REPORT_STATUS, REPORT_STATUS_TEXT } from '@/enum'
import {
  queryList,
} from '@/api/modules/enterprise'
import { dataReportQueryList, reportDataById, exportFileOfReport } from '@/api/modules/dataReport'
import { loadEnterpriseOfReport } from '@/api/modules/sysDictionaryEnco'

const userStore = useUserStore()

export interface ListItem {
  id?: number
  name: string
  tId: REPORT_ID
  status: REPORT_STATUS
  remark: string
}

export interface QueryListParams {
  time: string
  socialCreditCode: string
  subsidiaryType: any
  /** 0,1,3 */
  type: any
  status?: REPORT_STATUS
}

export interface SaveFnParams {
  socialCreditCode: string
  subsidiaryName: string
  subsidiaryRegion: string
  year: number
  month: number
  id: number | undefined
  userName: string | undefined
}

// 查询企业基本信息
// 如果企业缺失名称或社会信用代码或所属区域之一
// 提示完善信息
export async function checkEnterpriseInfo(user: any, successFn: () => void) {
  const userInfo = user || userStore.userInfo
  if (userInfo.tenantId) {
    const { data } = await queryList(userInfo.tenantId)
    const [d] = data
    if (d) {
      const $confirm = ElMessageBox.confirm
      const { enterpriseName, districtBelong, socialCreditCode } = d
      if (!enterpriseName || !socialCreditCode || !districtBelong) {
        $confirm('企业信息不完整，请完善信息').then(successFn).catch(() => {})
        return null
      }
      return {
        enterpriseName,
        districtBelong,
        socialCreditCode,
      }
    }
  }
  return null
}

export function useLoadData() {
  const tableData = ref<ListItem[]>([])
  const loading = ref(false)
  const userStore = useUserStore()

  // 查询企业配置的下拉列表
  async function getEnterpriseListOfReport() {
    const { data } = await loadEnterpriseOfReport()
    return data
  }

  async function queryList(params: QueryListParams) {
    loading.value = true
    try {
      const { data } = await dataReportQueryList(
        params.time,
        params.socialCreditCode,
        params.subsidiaryType,
        params.type,
        params.status,
      )
      const ret = [] as ListItem[]
      if (Array.isArray(data.dataList)) {
        for (const item of data.dataList) {
          ret.push({
            id: item.id,
            name: item.reportName,
            tId: item.reportId,
            status: +item.status,
            remark: item.remark,
          })
        }
      }
      tableData.value = ret
    }
    finally {
      loading.value = false
    }
  }

  function getStatusText(status: REPORT_STATUS) {
    let result = '' as REPORT_STATUS_TEXT
    switch (status) {
      case REPORT_STATUS.UNDER_REPORT:
        result = REPORT_STATUS_TEXT.UNDER_REPORT
        break
      case REPORT_STATUS.STAGED:
        result = REPORT_STATUS_TEXT.STAGED
        break
      case REPORT_STATUS.REPORTED:
        result = REPORT_STATUS_TEXT.REPORTED
        break
      case REPORT_STATUS.RESOLVED:
        result = REPORT_STATUS_TEXT.RESOLVED
        break
      case REPORT_STATUS.REJECTED:
        result = REPORT_STATUS_TEXT.REJECTED
        break
    }
    return result
  }

  // 按钮 actions 逻辑 填写-1，修改-2，查看-3，上报-4，历史操作-5，导出-6
  // 未填写 -> 填写                           => [1]
  // 已填写 -> 查看、修改、上报、历史操作、导出       => [2, 3, 4, 5, 6]
  // 已上报 -> 查看、历史操作、导出            => [3, 5, 6]
  // 已审核 -> 查看、历史操作、导出           => [3, 5, 6]
  // 退回   -> 查看、修改、历史操作、导出     => [2, 3, 5, 6]

  function showAction(status: REPORT_STATUS, t: number): boolean {
    switch (status) {
      case REPORT_STATUS.UNDER_REPORT:
        return ([1]).includes(t)
      case REPORT_STATUS.STAGED:
        return ([2, 3, 4, 5, 6]).includes(t)
      case REPORT_STATUS.REPORTED:
        return ([3, 5, 6]).includes(t)
      case REPORT_STATUS.RESOLVED:
        return ([3, 5, 6]).includes(t)
      case REPORT_STATUS.REJECTED:
        return ([2, 3, 5, 6]).includes(t)
      default:
        return true
    }
  }

  // 上报报表
  async function report(id: number) {
    return await reportDataById(id, userStore.userInfo.userName as string)
  }

  async function exportFile(id: number) {
    exportFileOfReport(id)
  }

  const designStore = useDesignStore()

  function getTagColor(status: REPORT_STATUS): string {
    switch (status) {
      case REPORT_STATUS.UNDER_REPORT:
        return 'yellow'
      case REPORT_STATUS.STAGED:
        return designStore.isDark ? 'white' : 'black'
      case REPORT_STATUS.REPORTED:
        return 'green'
      case REPORT_STATUS.RESOLVED:
        return 'blue'
      case REPORT_STATUS.REJECTED:
        return 'red'
    }
  }

  // 如果选

  return {
    loading,
    tableData,
    queryList,
    getStatusText,
    showAction,
    getEnterpriseListOfReport,
    report,
    exportFile,
    getTagColor,
  }
}
